﻿Public Class SoftWareList

    Private _typeid As String = ""
    Private _更新 As Boolean = False

    Private _lstS As New List(Of SoftWareEdit)
    Public Property 是否更新() As Boolean
        Get
            Return _更新
        End Get
        Set(ByVal value As Boolean)
            _更新 = value
        End Set
    End Property

    Public Sub New(ByVal typeid As String)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        _typeid = typeid
    End Sub

    Private Sub SoftWareList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        InitSoftWare()
        AddHandler p1.MouseWheel, AddressOf sc1_Panel2_MouseMove
    End Sub

    Private Sub InitSoftWare()

        Dim startTop As Integer = 7
        Dim startLeft As Integer = 2

        Dim ty As New XMLSoftWare
        Dim cws As List(Of CSoftWare)
        If _typeid.Trim = "" Then
            cws = ty.GetAll
        Else
            cws = ty.GetByTypeId(_typeid)
        End If

        p1.Controls.Clear()
        _lstS.Clear()
        Me.SuspendLayout()
        For index As Integer = 0 To cws.Count - 1
            Dim sy As New SoftWareEdit
            sy.软件 = cws(index)
            sy.Top = startTop + index * (50 + 5)
            sy.Height = 50
            sy.Left = startLeft
            sy.Width = p1.Width - 6
            AddHandler sy.EditSoftWare, AddressOf 修改Click
            AddHandler sy.DelSoftWare, AddressOf 删除Click
            sy.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            p1.Controls.Add(sy)
            _lstS.Add(sy)
        Next
        Me.ResumeLayout(True)
    End Sub

    Private Sub 添加软件ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 添加软件ToolStripMenuItem.Click
        Dim ty As New CSoftWare
        Dim tys As New XMLSoftWare
        ty.Id = tys.GetMaxid
        Dim lsttype As New List(Of String)
        lsttype.Add(_typeid)

        ty.TypeId = lsttype
        Using frm As New SoftWareAdd(ty)
            frm.Text = "添加软件"
            frm.ShowDialog()
            If frm.更新 = True Then
                InitSoftWare()
                _更新 = True
            End If
        End Using
    End Sub

    Private Sub 修改Click(ByVal typeid As String)

        Dim ty As New XMLSoftWare
        Dim rj As CSoftWare = ty.GetById(typeid)
        Dim lsttype As New List(Of String)
        lsttype.Add(_typeid)

        rj.TypeId = lsttype
        Using frm As New SoftWareAdd(rj)
            frm.Text = "修改软件"
            frm.ShowDialog()
            If frm.更新 = True Then
                InitSoftWare()
                _更新 = True
            End If
        End Using
    End Sub

    Private Sub 删除Click(ByVal typeid As String)

        If wys.util.Common.MessageBase.ShowMessage("是否删除次软件？", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
            Dim ty As New XMLSoftWare
            ty.Delete(typeid)
            InitSoftWare()
            _更新 = True
        End If
    End Sub

    Private Sub p1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles p1.Scroll
        For index As Integer = 0 To p1.Controls.Count - 1
            p1.Controls(index).Width = p1.Width - 6 - 17
        Next
    End Sub

    Private Sub sc1_Panel2_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If _lstS.Count > 0 Then
            If _lstS(_lstS.Count - 1).Height + _lstS(_lstS.Count - 1).Top > Me.Height Then
                For index As Integer = 0 To p1.Controls.Count - 1
                    p1.Controls(index).Width = p1.Width - 6 - 17
                Next
            End If
        End If
    End Sub

End Class
